﻿using System;
using System.Web.UI;
using Bingosoft.Modules.Demo.User;
using Bingosoft.Web.Utils;
using HHMMS.Core.Service;
using HHMMS.Core.Model;

namespace HHMMS.UI.Modules.MaterialDirectory
{
    public partial class MaterialDirectoryEdit : FormPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack && !SmartForm.IsInsert())
            {
                var param = MaterialDirectoryService.Instance.Single(Request.GetQuery<string>("MaterialsNo"));
                SmartForm.Fill(container, param);
                this.tbMaterialsNo.ReadOnly = true;
            }
        }

        protected void SaveOrUpdateUser(object sender, EventArgs e)
        {
            HHMMS.Core.Model.MaterialDirectory param = null;
            if (SmartForm.IsInsert())
            {
                param = MaterialDirectoryService.Instance.Single(this.tbMaterialsNo.Text.Trim());
                if (param != null && !string.IsNullOrEmpty(param.MaterialsNo))
                {
                    ScriptHelper.Alert(this, "物资编码[" + this.tbMaterialsNo.Text + "]已存在，请更改后再试！");
                    return;
                }
            }

            param = SmartForm.GetEntity<HHMMS.Core.Model.MaterialDirectory>(container);
            MaterialDirectoryService.Instance.SaveOrUpdate(param);
            ScriptHelper.CloseDialog(this, "true");
        }
    }
}